// 2024/5/22: 首个版本
// 2024/6/25: 支持API3

using System;
using ASEva;
using ASEva.UIEto;
using Eto.Forms;
using Eto.Drawing;

namespace CommonSource
{
    class DataEncryptionConfigDialog : DialogPanel
    {
        public DataEncryptionConfigDialog(WorkflowEncryptionCallback callback, Icon icon = null)
        {
            this.callback = callback;

            Icon = icon;
            Title = chinese ? "数据加密/解密配置" : "Data encryption key";
            this.SetFixMode(400, 60, true);

            var mainLayout = this.SetContentAsRowLayout();
            mainLayout.AddLabel(chinese ? "密码 :" : "Password :");
            textBox = mainLayout.AddControl(new PasswordBox{ PasswordChar = '●' }, true) as PasswordBox;
            var overlay = mainLayout.AddControl(new OverlayLayout(), false, 120) as OverlayLayout;
            var checkShow = new CheckBox{ Text = chinese ? "显示密码" : "Show password" };
            var buttonUnlock = new Button{ Text = chinese ? "解锁" : "Unlock", Enabled = false };

            checkShow.CheckedChanged += delegate
            {
                textBox.PasswordChar = checkShow.Checked.Value ? (char)0 : '●';
            };

            textBox.KeyUp += (o, e) =>
            {
                if (e.Key == Keys.Enter && textBox.Text.Length > 0)
                {
                    if (locking)
                    {
                        if (buttonUnlock.Enabled) buttonUnlock.PerformClick();
                    }
                    else
                    {
                        callback.UpdateDataKey(textBox.Text);
                        this.Close();
                    }
                }

                if (!locking && textBox.Text.Length == 0) textBox.Text = "default";
            };

            textBox.TextChanged += delegate
            {
                if (locking)
                {
                    buttonUnlock.Enabled = callback.UnlockDataKey(textBox.Text);
                }
            };

            buttonUnlock.Click += delegate
            {
                overlay.RemoveControl(buttonUnlock);
                overlay.AddControl(checkShow, null, null, 0, null);
                locking = false;
            };

            timer = new UITimer();
            timer.Interval = 0.001;
            timer.Elapsed += delegate
            {
                timer.Stop();
                if (callback.UnlockDataKey(null))
                {
                    textBox.Text = "default";
                    overlay.AddControl(checkShow, null, null, 0, null);
                }
                else
                {
                    locking = true;
                    overlay.AddControl(buttonUnlock, null, null, 0, 0);
                }
            };
            timer.Start();
        }

        public override void OnClosing()
        {
            if (locking) return;
            if (textBox.Text.Length > 0) callback.UpdateDataKey(textBox.Text);
        }

        private UITimer timer;
        private PasswordBox textBox;
        private WorkflowEncryptionCallback callback;
        private bool locking = false;

        private bool chinese = AgencyLocal.GetAppLanguage() == Language.Chinese;
    }
}